package org.oos.dao.impl;

import static com.google.api.client.http.HttpMethod.POST;
import static org.oos.ApiUrlProvider.UserCheckin;
import static org.oos.atom.AtomService.atomService;
import static org.oos.dao.options.OptionsMap.FINDERS.CHECKIN_OPTIONS;
import static org.oos.dao.options.OptionsMap.FINDERS.FIELD_OPTIONS;

import org.oos.atom.AtomRequest;
import org.oos.atom.AtomRequest.Builder;
import org.oos.atom.AtomResponse;
import org.oos.atom.AtomService.HttpStatusException;
import org.oos.atom.CheckinFeed;
import org.oos.atom.entries.CheckinEntry;
import org.oos.atom.entries.impl.CheckinEntryImpl;
import org.oos.atom.impl.CheckinFeedImpl;
import org.oos.dao.UserCheckinDao;
import org.oos.dao.exceptions.CreationException;
import org.oos.dao.exceptions.ReadException;
import org.oos.dao.lists.impl.ReadOnlyDynamicList;
import org.oos.dao.options.CheckinOptions;
import org.oos.dao.options.CheckinOptions.CheckinRequestType;
import org.oos.dao.options.FieldOptions;
import org.oos.lists.DynamicList;

import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpParser;
import com.google.api.client.http.UrlEncodedContent;
import com.google.api.client.http.UrlEncodedParser;

/**
 * 
 * Checkin DAO
 * 
 * @author pablo.gonzalez@11870.com
 * 
 */
public class UserCheckinDaoImpl extends
		AbstractDao<CheckinEntry, CheckinFeed<CheckinEntry>> implements
		UserCheckinDao {

	private static final long serialVersionUID = -8715610759553023164L;
	private String userSlug;

	public UserCheckinDaoImpl() {
		super(CheckinFeedImpl.class, CheckinEntryImpl.class);
	}

	@Override
	public GenericUrl getFeedUrl() {
		return UserCheckin.getUrl(userSlug);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	protected ReadOnlyDynamicList<CheckinEntry, CheckinFeed<CheckinEntry>> getList(
			CheckinFeed feed) {
		return new ReadOnlyDynamicList<CheckinEntry, CheckinFeed<CheckinEntry>>(
				this, feed);
	}

	@Override
	public void setUserSlug(String userSlug) {
		this.userSlug = userSlug;
	}

	@SuppressWarnings("unchecked")
	@Override
	public DynamicList<CheckinEntry> read() throws ReadException {
		FieldOptions options = FIELD_OPTIONS.get(getOptions());
		options.clear();
		options.add("updated");
		options.add("author");
		options.add("title");
		options.add("oos:count");
		options.add("oos:slug");
		options.add("content");
		CheckinOptions checkinOptions = CHECKIN_OPTIONS.get(getOptions());
		checkinOptions.setType(CheckinRequestType.GROUPED);
		return super.read();
	}

	protected Builder requestBuilder(HttpParser parser) {
		return AtomRequest.requestBuilder() //
				.setAuthHandler(authHandler) //
				.setParameters(getOptions()) //
				.setHttpParser(parser); //
	}
	@Override
	public CheckinEntry create() throws CreationException {
		try{
		UrlEncodedContent content = new UrlEncodedContent(null);
		AtomRequest request = requestBuilder(new UrlEncodedParser()) //
				.setUrl(getFeedUrl()) //
				.setContent(content) //
				.setHttpMethod(POST) //
				.build(); //
		
		AtomResponse<CheckinEntry> response;
		response = atomService().execute(request, getEntryType());
		switch (response.getType()){
		case Error:
			throw response.getThrowable();
		case Empty:
			/**Returning null on create for the empty response*/
			return null;
		case Unknown:
		default:
			throw new IllegalArgumentException("Unknown error");
		}
	} catch (Throwable e) {
		CreationException exception;
		if (e instanceof HttpStatusException) {
			exception = new CreationException(e.getMessage());
			exception.setResponse(((HttpStatusException) e).getResponse());
		} else {
			exception = new CreationException(e);
		}
		throw exception;
	}
	}
	
	

}
